package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.PowerPlantModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 电厂业务
 * @author zhaokf
 * @date 2013-04-19
 */
public class PowerPlantService extends BaseService
{
	/**
	 * 分页列出电厂
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<PowerPlantModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?10:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 10;
			page = 1;
		}
		StringBuffer where = new StringBuffer();;
		try{
			if (search.get("pp_addr") != null){
				where.append(" AND pp_addr LIKE '%" + search.get("pp_addr") + "%'");
			}
			if (search.get("pp_name") != null){
				where.append(" AND pp_name LIKE '%" + search.get("pp_name") + "%'");
			}
		}catch (Exception e){
		}
		Page<PowerPlantModel> PP = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM C_PP WHERE 1=1 " + where;
		try{
			PP = PowerPlantModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return PP;
	}


	/**
	 * 根据ID查询电厂
	 * @param params
	 * @return
	 */
	public static PowerPlantModel get(Map<String,String> params){
		String no = params.get("pp_id")==null?"":params.get("pp_id").toString();
		PowerPlantModel pp = PowerPlantModel.dao.findFirst("SELECT * FROM C_PP WHERE PP_ID=?", no);
		return pp;
	}

	/**
	 * 增加电厂信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		Record record = new Record();
		record.set("PP_ID", Db.queryBigDecimal("select c_pp_seq.nextval from dual").intValue());
		record.set("PP_CODE", params.get("pp_code").toString());
		record.set("PP_NAME", params.get("pp_name").toString());
		record.set("PP_ORG_NO", params.get("pp_org_no").toString());
		boolean isupadded = Db.save("C_PP", "PP_ID",record);
		if(isupadded){
			return true;
		}
		return setError("保存权限信息失败！");
		
	}

	/**
	 * 修改电厂信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String pp_id = params.get("pp_id").toString();
		Record record = new Record();
		record.set("PP_ID",pp_id);
		record.set("PP_CODE",params.get("pp_code"));
		record.set("PP_NAME", params.get("pp_name"));
		record.set("PP_ORG_NO", params.get("pp_org_no"));
		boolean isupdated = Db.update("C_PP", "PP_ID",record);
		if(isupdated){
			return true;
		}
		return setError("您访问的电厂点存在！");
	}

	/**
	 * 删除电厂信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("pp_id")==null?"":params.get("pp_id").toString();
		boolean deleted = Db.deleteById("C_PP", "PP_ID", no);
		if (deleted){
			return true;
		}
		return setError("您访问的电厂不存在！");
	}
}

